package org.toonan.rental.chart;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class BarChartActivity extends Activity implements View.OnClickListener,
		AdapterView.OnItemClickListener
{
	private static final int width = 20;
	private Animation animation;
	BaseAdapter barAdapter;
	List<Integer> barColors;
	private int[] barColors1 = new int[this.yScale.length];
	private int[] barColors2 = new int[this.yScale.length];
	ListView listView;
	private HorizontalScrollView scrollview;
	private int selected = -1;
	private List<View> vArrays;
	private BarChartView[] views = new BarChartView[this.yScale.length];
	private BarChartView[] views2 = new BarChartView[this.yScale.length];
	private List<String> xArrays;
	private String[] xScale = { "黄阁镇", "横沥镇", "南岗镇", "人和镇", "蚌湖镇", "江高镇", "狮岭镇" };
	private String[] xScale1 = { "黄阁镇房屋套数", "横沥镇房屋套数", "南岗镇房屋套数", "人和镇房屋套数", "蚌湖镇房屋套数", "江高镇房屋套数", "狮岭镇房屋套数" };
	private String[] xScale2 = { "黄阁镇流动人数", "横沥镇流动人数", "南岗镇流动人数", "人和镇流动人数", "蚌湖镇流动人数", "江高镇流动人数", "狮岭镇流动人数" };
	private List<Float> yArrays;
	private float[] yScale = { 60.0F, 80.0F, 50.0F, 30.0F, 10.0F, 40.0F, 70.0F };
	private float[] yScale2 = { 75.0F, 90.0F, 65.0F, 37.0F, 30.0F, 20.0F, 90.0F };
	
	protected void onCreate(Bundle paramBundle)
	{
		super.onCreate(paramBundle);
		setContentView(R.layout.bar_chart);
		initButton();
		createChartlineView();
		createBarChartView();
		initListView();
	}
	
	private void createChartlineView()
	{
		((LinearLayout) findViewById(R.id.chartline)).addView(new ChartLineView(this),
				new ViewGroup.LayoutParams(740, 350));
	}
	
	private void initButton()
	{
		int i = 0;
		Button[] arrayOfButton = { (Button) findViewById(R.id.button1), (Button) findViewById(R.id.button2),
				(Button) findViewById(R.id.button3), (Button) findViewById(R.id.button4) };
		ButtonListener localButtonListener = new ButtonListener(this);
		int j = arrayOfButton.length;
		while (true)
		{
			if (i >= j)
				return;
			arrayOfButton[i].setOnClickListener(localButtonListener);
			i++;
		}
	}
	
	private void initData()
	{
		this.xArrays = new ArrayList();
		this.yArrays = new ArrayList();
		this.vArrays = new ArrayList();
		this.barColors = new ArrayList();
		for (int i = 0;; i++)
		{
			if (i >= this.xScale1.length)
				return;
			this.xArrays.add(this.xScale1[i]);
			this.xArrays.add(this.xScale2[i]);
			this.yArrays.add(Float.valueOf(this.yScale[i]));
			this.yArrays.add(Float.valueOf(this.yScale2[i]));
			this.vArrays.add(this.views[i]);
			this.vArrays.add(this.views2[i]);
			this.barColors.add(Integer.valueOf(this.barColors1[i]));
			this.barColors.add(Integer.valueOf(this.barColors2[i]));
		}
	}
	
	private void initListView()
	{
		this.listView = ((ListView) findViewById(2131230732));
		this.barAdapter = new BarAdapter();
		this.listView.setAdapter(this.barAdapter);
		this.animation = AnimationUtils.loadAnimation(this, 2130968576);
		this.listView.setOnItemClickListener(this);
	}
	
	public void onClick(View paramView)
	{
		this.selected = paramView.getId();
		this.listView.setAdapter(this.barAdapter);
		this.listView.smoothScrollToPosition(this.selected);
		((View) this.vArrays.get(this.selected)).startAnimation(this.animation);
	}
	
	public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong)
	{
		((View) this.vArrays.get(paramInt)).startAnimation(this.animation);
		this.selected = paramInt;
		this.listView.setAdapter(this.barAdapter);
		this.listView.setSelection(this.selected);
		this.scrollview.scrollTo(20 * this.selected, 0);
	}
	
	private void createBarChartView()
	{
		LinearLayout localLinearLayout1 = (LinearLayout) findViewById(R.id.barChartLayout);
		LinearLayout localLinearLayout2 = (LinearLayout) findViewById(R.id.barChartLayout2);
		int i = 0;
		LinearLayout.LayoutParams localLayoutParams;
		int j;
		if (i >= this.yScale.length)
		{
			initData();
			localLayoutParams = new LinearLayout.LayoutParams(100, 400);
			localLayoutParams.setMargins(0, 0, 35, 0);
			j = 0;
			for (int k = 0;; k++)
			{
				if (k >= this.vArrays.size())
				{
					this.scrollview = ((HorizontalScrollView) findViewById(R.id.hscrollview));
					this.views[i] = new BarChartView(this, this.yScale[i], this.xScale[i], i, true);
					this.views2[i] = new BarChartView(this, this.yScale2[i], "", i, true);
					this.barColors1[i] = this.views[i].getColor(i);
					this.barColors2[i] = this.views2[i].getColor(i);
					i++;
					localLinearLayout1.addView(this.views[j], localLayoutParams);
					localLinearLayout2.addView(this.views2[j], localLayoutParams);
					j++;
				}
				if (j < this.views.length)
					break;
				((View) this.vArrays.get(k)).setId(k);
				((View) this.vArrays.get(k)).setOnClickListener(this);
			}
		}
	}
	
	private class BarAdapter extends BaseAdapter
	{
		private BarAdapter()
		{
		}
		
		public int getCount()
		{
			return BarChartActivity.this.xArrays.size();
		}
		
		public Object getItem(int paramInt)
		{
			return Integer.valueOf(paramInt);
		}
		
		public long getItemId(int paramInt)
		{
			return paramInt;
		}
		
		public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
		{
			Viewholder localViewholder;
			if (paramView == null)
			{
				LinearLayout localLinearLayout = (LinearLayout) LayoutInflater.from(BarChartActivity.this)
						.inflate(R.layout.barchart_item, null);
				localViewholder = new Viewholder();
				localViewholder.image = ((ImageView) localLinearLayout.findViewById(R.id.imageView1));
				localViewholder.text1 = ((TextView) localLinearLayout.findViewById(R.id.textView1));
				localViewholder.text2 = ((TextView) localLinearLayout.findViewById(R.id.textView2));
				localLinearLayout.setTag(localViewholder);
				paramView = localLinearLayout;
			}
			localViewholder = (Viewholder) ((LinearLayout) paramView).getTag();
			localViewholder.image
					.setBackgroundColor(((Integer) BarChartActivity.this.barColors.get(paramInt)).intValue());
			localViewholder.text1.setText(String.valueOf(BarChartActivity.this.yArrays.get(paramInt)));
			localViewholder.text2.setText((CharSequence) BarChartActivity.this.xArrays.get(paramInt));
			if (BarChartActivity.this.selected == paramInt)
				paramView.setBackgroundResource(R.drawable.item_selection);
			return paramView;
		}
		
		private class Viewholder
		{
			ImageView image;
			TextView text1;
			TextView text2;
			
			private Viewholder()
			{
			}
		}
	}
	
}